package day4;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author joker
 * @date 2022/4/15 0015 - 1:58
 */
public class Window2 implements Runnable{
    private int ticket=100;
    private Lock lock=new ReentrantLock();
    @Override
    public void run() {
        sale();
    }

    private void sale() {
        while(true){
            try {
                lock.lock();
                if (ticket > 0) {

                    System.out.println(Thread.currentThread().getName() + ":卖票，票号为：" + ticket);
                    ticket--;

                } else {
                    break;
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
